package com.leetcode.LC;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class LC1636 {//来自网友
    public int[] frequencySort(int[] nums) {
        Map<Integer, Integer> map = new HashMap<>();

        for (int x : nums) {
            map.put(x, map.getOrDefault(x, 0) + 1);
        }
        List<Integer> arr = new ArrayList<>(map.keySet());
        arr.sort((a, b)->{
            if (map.get(a) == map.get(b)) {
                return b - a;
            }
            return map.get(a) - map.get(b);
        });

        int index = 0;
        for (int x : arr) {
            int cnt = map.get(x);
            while (cnt > 0) {
                nums[index++] = x;
                cnt--;
            }
        }

        return nums;
    }
}
